package com.aaa.tars.service.impl;

import com.aaa.tars.dao.AttractionAuditDao;
import com.aaa.tars.entity.AttractionAudit;
import com.aaa.tars.entity.ScenicAudit;
import com.aaa.tars.entity.User;
import com.aaa.tars.service.AttractionAuditService;
import com.aaa.tars.util.CustomException;
import com.aaa.tars.util.Page;
import com.aaa.tars.util.ResultStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @FileName: AttractionAuditServiceImpl
 * @Description:
 * @Author: xm
 * @CreateTime: 2025/9/28 15:49
 * @Version: 1.0.0
 */
@Service
public class AttractionAuditServiceImpl implements AttractionAuditService {
    @Resource
    private AttractionAuditDao attractionAuditDao;
    @Resource
    private HttpSession session;
    @Override
    public AttractionAudit queryById(Integer auditId) {
        if(auditId==null||auditId<0){
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(),ResultStatus.PARAM_ISNULL.getStatusMessage());
        }
        return attractionAuditDao.queryById(auditId);
    }

    @Override
    public AttractionAudit queryByDotId(Integer dotId) {
        if(dotId==null||dotId<0){
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(),ResultStatus.PARAM_ISNULL.getStatusMessage());
        }
        return attractionAuditDao.queryByDotId(dotId);
    }

    @Override
    public PageInfo queryByPage(Page<AttractionAudit> page) {
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();

        if(pageNo==null||pageNo==0||pageSize==null||pageSize==0){
            throw  new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(),ResultStatus.PARAM_ISNULL.getStatusMessage());
        }
        PageHelper.startPage(pageNo,pageSize);
        AttractionAudit attractionAudit = page.getData();
        List<AttractionAudit> attractionAuditList = attractionAuditDao.queryAllByLimit(attractionAudit);
        return new PageInfo(attractionAuditList);
    }

    @Override
    public int insert(AttractionAudit attractionAudit) {
        return attractionAuditDao.insert(attractionAudit);

    }

    @Override
    public int update(AttractionAudit attractionAudit) {
        //动态获取创建人
        //拿到登录session
        User userInfo = (User) session.getAttribute("userInfo");
        attractionAudit.setAuditPeople(userInfo.getUserName());
        // 设置更新时间
        attractionAudit.setAuditTime(LocalDateTime.now());
        //如果状态为通过，清空拒绝理由
        if(attractionAudit.getAuditStatus().equals("1")){
            attractionAudit.setRejectReason("");
        }
        return attractionAuditDao.update(attractionAudit);
    }

    @Override
    public int updateByDotId(AttractionAudit attractionAudit) {
        return attractionAuditDao.updateByDotId(attractionAudit);
    }

    @Override
    public int deleteById(Integer auditId) {
        if(auditId==null||auditId<0){
            throw new CustomException(ResultStatus.PARAM_ISNULL.getStatusCode(),ResultStatus.PARAM_ISNULL.getStatusMessage());
        }
        //如果待审核状态为待审核，则不能删除
        AttractionAudit attractionAudit = attractionAuditDao.queryById(auditId);
        if(attractionAudit.getAuditStatus().equals("0")){
            //抛出报错信息
            throw new CustomException(1020,"未审核，不能删除");
        }
        //返回设置删除状态为1
        attractionAudit.setDelFlag("1");
        return attractionAuditDao.update(attractionAudit);
    }
}
